//
//  GTListItem.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/14.
//

#import "GTListItem.h"
#import <YYModel/YYModel.h>
NSString *const kRootClassAuthorName = @"author_name";
NSString *const kRootClassCategory = @"category";
NSString *const kRootClassDate = @"date";
NSString *const kRootClassThumbnailPicS = @"thumbnail_pic_s";
NSString *const kRootClassThumbnailPicS02 = @"thumbnail_pic_s02";
NSString *const kRootClassThumbnailPicS03 = @"thumbnail_pic_s03";
NSString *const kRootClassTitle = @"title";
NSString *const kRootClassUniquekey = @"uniquekey";
NSString *const kRootClassUrl = @"url";

@implementation GTListItem

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
	self = [super init];
	if(![dictionary[kRootClassAuthorName] isKindOfClass:[NSNull class]]){
		self.authorName = dictionary[kRootClassAuthorName];
	}
	if(![dictionary[kRootClassCategory] isKindOfClass:[NSNull class]]){
		self.category = dictionary[kRootClassCategory];
	}
	if(![dictionary[kRootClassDate] isKindOfClass:[NSNull class]]){
		self.date = dictionary[kRootClassDate];
	}
	if(![dictionary[kRootClassThumbnailPicS] isKindOfClass:[NSNull class]]){
		self.thumbnailPicS = dictionary[kRootClassThumbnailPicS];
	}
	if(![dictionary[kRootClassThumbnailPicS02] isKindOfClass:[NSNull class]]){
		self.thumbnailPicS02 = dictionary[kRootClassThumbnailPicS02];
	}
	if(![dictionary[kRootClassThumbnailPicS03] isKindOfClass:[NSNull class]]){
		self.thumbnailPicS03 = dictionary[kRootClassThumbnailPicS03];
	}
	if(![dictionary[kRootClassTitle] isKindOfClass:[NSNull class]]){
		self.title = dictionary[kRootClassTitle];
	}
	if(![dictionary[kRootClassUniquekey] isKindOfClass:[NSNull class]]){
		self.uniquekey = dictionary[kRootClassUniquekey];
	}
	if(![dictionary[kRootClassUrl] isKindOfClass:[NSNull class]]){
		self.url = dictionary[kRootClassUrl];
	}
	return self;
}


/**
 * Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
 */
-(NSDictionary *)toDictionary
{
	NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
	if(self.authorName != nil){
		dictionary[kRootClassAuthorName] = self.authorName;
	}
	if(self.category != nil){
		dictionary[kRootClassCategory] = self.category;
	}
	if(self.date != nil){
		dictionary[kRootClassDate] = self.date;
	}
	if(self.thumbnailPicS != nil){
		dictionary[kRootClassThumbnailPicS] = self.thumbnailPicS;
	}
	if(self.thumbnailPicS02 != nil){
		dictionary[kRootClassThumbnailPicS02] = self.thumbnailPicS02;
	}
	if(self.thumbnailPicS03 != nil){
		dictionary[kRootClassThumbnailPicS03] = self.thumbnailPicS03;
	}
	if(self.title != nil){
		dictionary[kRootClassTitle] = self.title;
	}
	if(self.uniquekey != nil){
		dictionary[kRootClassUniquekey] = self.uniquekey;
	}
	if(self.url != nil){
		dictionary[kRootClassUrl] = self.url;
	}
	return dictionary;

}

/**
 * Implementation of NSCoding encoding method
 */
/**
 * Returns all the available property values in the form of NSDictionary object where the key is the approperiate json key and the value is the value of the corresponding property
 */
#pragma mark - NSSecureCoding
- (void)encodeWithCoder:(NSCoder *)aCoder
{
	if(self.authorName != nil){
		[aCoder encodeObject:self.authorName forKey:kRootClassAuthorName];
	}
	if(self.category != nil){
		[aCoder encodeObject:self.category forKey:kRootClassCategory];
	}
	if(self.date != nil){
		[aCoder encodeObject:self.date forKey:kRootClassDate];
	}
	if(self.thumbnailPicS != nil){
		[aCoder encodeObject:self.thumbnailPicS forKey:kRootClassThumbnailPicS];
	}
	if(self.thumbnailPicS02 != nil){
		[aCoder encodeObject:self.thumbnailPicS02 forKey:kRootClassThumbnailPicS02];
	}
	if(self.thumbnailPicS03 != nil){
		[aCoder encodeObject:self.thumbnailPicS03 forKey:kRootClassThumbnailPicS03];
	}
	if(self.title != nil){
		[aCoder encodeObject:self.title forKey:kRootClassTitle];
	}
	if(self.uniquekey != nil){
		[aCoder encodeObject:self.uniquekey forKey:kRootClassUniquekey];
	}
	if(self.url != nil){
		[aCoder encodeObject:self.url forKey:kRootClassUrl];
	}

}

/**
 * Implementation of NSCoding initWithCoder: method
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
	self = [super init];
	self.authorName = [aDecoder decodeObjectForKey:kRootClassAuthorName];
	self.category = [aDecoder decodeObjectForKey:kRootClassCategory];
	self.date = [aDecoder decodeObjectForKey:kRootClassDate];
	self.thumbnailPicS = [aDecoder decodeObjectForKey:kRootClassThumbnailPicS];
	self.thumbnailPicS02 = [aDecoder decodeObjectForKey:kRootClassThumbnailPicS02];
	self.thumbnailPicS03 = [aDecoder decodeObjectForKey:kRootClassThumbnailPicS03];
	self.title = [aDecoder decodeObjectForKey:kRootClassTitle];
	self.uniquekey = [aDecoder decodeObjectForKey:kRootClassUniquekey];
	self.url = [aDecoder decodeObjectForKey:kRootClassUrl];
	return self;

}

+ (BOOL) supportsSecureCoding {
	return YES;
}

/**
 * Implementation of NSCopying copyWithZone: method
 */
- (instancetype)copyWithZone:(NSZone *)zone
{
	GTListItem *copy = [GTListItem new];

	copy.authorName = [self.authorName copy];
	copy.category = [self.category copy];
	copy.date = [self.date copy];
	copy.thumbnailPicS = [self.thumbnailPicS copy];
	copy.thumbnailPicS02 = [self.thumbnailPicS02 copy];
	copy.thumbnailPicS03 = [self.thumbnailPicS03 copy];
	copy.title = [self.title copy];
	copy.uniquekey = [self.uniquekey copy];
	copy.url = [self.url copy];

	return copy;
}
- (NSString *)description{
	return [self yy_modelDescription];
}

#pragma mark - IGListDiffable
- (nonnull id<NSObject>)diffIdentifier{
	return self.uniquekey;
}

- (BOOL)isEqualToDiffableObject:(nullable id<IGListDiffable>)object{
	if(self == object){
		return YES;
	}
	
	if(![(NSObject *)object isKindOfClass:[GTListItem class]]){
		return NO;
	}
	
	return [self.uniquekey isEqualToString:((GTListItem *)object).uniquekey];
}
@end
